<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <h1>login</h1>

    <input type="text" class="username">
    <br>
    <input type="text" class="password">
    <br>
    <button class="btn">登陆</button>
    <script>


        const oUser = document.querySelector('.username') ;
        const oPwd = document.querySelector('.password') ;
        const oBtn = document.querySelector('.btn') ;

        oBtn.onclick = function() {
            const username = oUser.value ;
            const password = oPwd.value ;
            const xhr2 = new XMLHttpRequest();

            xhr2.open('post', '/api/users/login', true);
            xhr2.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr2.send(`username=${username}&password=${password}`);
            // xhr2.send() ;

            xhr2.onreadystatechange = function () {
                if (xhr2.readyState === 4 && xhr2.status === 200) {
                    console.log(xhr2.responseText);
                    let data = xhr2.responseText ;
                    data = JSON.parse(data) ;
                    if(data.code === 1) {
                        // 去修改密码
                        localStorage.setItem('id' , data.id) ;
                        localStorage.setItem('token' , data.token);
                        location.href = './rpwd.html' ;
                    } else {
                        alert(data.msg);
                    }
                }
            }
        }

        
       

    </script>
    
</body>
</html>